package cn.hrlly.tao2shou.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.adapter.BuyerApplicationAdapter;
import cn.hrlly.tao2shou.domain.ApplicationList;
import cn.hrlly.tao2shou.domain.Applications;
import cn.hrlly.tao2shou.other.Config;
import cn.hrlly.tao2shou.presenter.ApplicationListPresenter;
import cn.hrlly.tao2shou.presenter.ApplicationListPresenterImpl;
import cn.hrlly.tao2shou.utils.NetUtil;
import cn.hrlly.tao2shou.view.base.BaseActivity;
import cn.hrlly.tao2shou.view.view_interface.ApplicationListView;

public class BuyerApplicationActivity extends BaseActivity implements ApplicationListView{

    @Bind(R.id.lvApplication)
    ListView lvApplication;
    private ApplicationListPresenter applicationListPresenter;
    private ArrayList<Applications> list;
    private BuyerApplicationAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buyer);
        //绑定控件
        ButterKnife.bind(this);
        //初始化
        init();
        //点击事件
        clickListview();
    }

    //初始化
    public void init(){
        applicationListPresenter=new ApplicationListPresenterImpl(this,this);
        list=new ArrayList<Applications>();
        if (NetUtil.ping(this)){
            applicationListPresenter.showApplicationList();
        }else {
            applicationListPresenter.showCacheApplicationList();
        }
    }

    //点击事件
    public void clickListview(){
        lvApplication.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(BuyerApplicationActivity.this,ApplicationDetailsActivity.class);
                intent.putExtra(Config.key_application_id,list.get(position).getApplicationId());
                startActivity(intent);
            }
        });
    }

    //以下是对activity的操作
    @Override
    public void showListView(ApplicationList applicationList) {
        list.addAll(applicationList.getApplications());
        adapter=new BuyerApplicationAdapter(list,this);
        lvApplication.setAdapter(adapter);
    }

    @Override
    public void showMessage(String errorMessage) {
        Toast.makeText(this,errorMessage,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showMessage(int errorMessage) {
        Toast.makeText(this,errorMessage,Toast.LENGTH_SHORT).show();
    }

    @Override
    public int getApplicationType() {
        //买家申请发送0
        return 0;
    }
}
